Imports Microsoft.VisualBasic
Imports System.Data
Imports System.Data.SqlClient


Public Class DBConnection

    
   

    Private Shared CONN_STRING = "Server=" & DBConnectionProperties._server _
    & "; DataBase=" & DBConnectionProperties._databaseName _
    & "; User ID=" & DBConnectionProperties._userName _
    & " ; Password= " + DBConnectionProperties._userPassword _
    '& " ; Pooling=false" '_
    '& " ; Connection Timeout=30" _
    '& "; Min Pool Size=10; Max Pool Size=20;"

    <Runtime.CompilerServices.MethodImpl(Runtime.CompilerServices.MethodImplOptions.Synchronized)> _
    Private Shared Function GET_OPEN_CONNECTION() As SqlConnection
        Try
            Dim MyConnection As New SqlConnection(CONN_STRING)

            MyConnection.Open()

            Return MyConnection
        Catch ex As Exception

        End Try
        Return Nothing
    End Function

    <Runtime.CompilerServices.MethodImpl(Runtime.CompilerServices.MethodImplOptions.Synchronized)> _
    Private Shared Function BUILD_COMMAND(ByVal procName As String, ByVal conn As SqlConnection, ByVal map As Hashtable) As SqlCommand

        Try
            Dim MyCommand As New SqlCommand(procName, conn)

            MyCommand.CommandType = CommandType.StoredProcedure

            For Each key As String In map.Keys
                MyCommand.Parameters.AddWithValue(key, map(key))
            Next

            Return MyCommand
        Catch ex As Exception

        End Try

        Return Nothing


    End Function

    <Runtime.CompilerServices.MethodImpl(Runtime.CompilerServices.MethodImplOptions.Synchronized)> _
    Public Sub DBCALL()

        Dim MyConnection As SqlConnection
        Dim MyDataReader As SqlDataReader
        Dim MyCommand As SqlCommand


        'Create a connection to the SQL Server.
        MyConnection = New SqlConnection(CONN_STRING)

        MyConnection.Open()

        MyCommand = New SqlCommand("STORE_PROC_NAME", MyConnection)

        MyCommand.CommandType = CommandType.StoredProcedure

        MyCommand.Parameters.AddWithValue("", "")

        MyDataReader = MyCommand.ExecuteReader()

        While (MyDataReader.Read())

            Dim val As String = CType(MyDataReader("COLUMN_NAME"), String)

        End While


        MyConnection.Close()

    End Sub

    <Runtime.CompilerServices.MethodImpl(Runtime.CompilerServices.MethodImplOptions.Synchronized)> _
    Public Sub STORE_PROC_NAME(ByVal arg1 As Object, ByVal arg2 As Object, ByVal arg3 As Object)

        Dim procName As String = "STORE_PROC_NAME"
        Dim map As New Hashtable

        map("@PARAM1") = arg1
        map("@PARAM2") = arg2
        map("@PARAM3") = arg3

        Dim MyConnection As SqlConnection
        Dim MyDataReader As SqlDataReader
        Dim MyCommand As SqlCommand


        MyConnection = New SqlConnection(CONN_STRING)

        MyConnection.Open()

        MyCommand = New SqlCommand(procName, MyConnection)

        MyCommand.CommandType = CommandType.StoredProcedure

        For Each key As String In map.Keys
            MyCommand.Parameters.AddWithValue(key, map(key))
        Next

        MyDataReader = MyCommand.ExecuteReader()

        While (MyDataReader.Read())

            Dim val As String = CType(MyDataReader("COLUMN_NAME"), String)

        End While


        MyConnection.Close()




    End Sub


#Region "SIMULATION"

    <Runtime.CompilerServices.MethodImpl(Runtime.CompilerServices.MethodImplOptions.Synchronized)> _
    Public Shared Sub INS_NEW_DEFAULT_SIMULATION(ByVal arg1 As Object, ByVal arg2 As Object, ByVal arg3 As Object, ByVal arg4 As Object, ByVal arg5 As Object)

        Dim procName As String = "INS_NEW_DEFAULT_SIMULATION"
        Dim map As New Hashtable

        map("@Name") = arg1
        map("@Desc") = arg2
        map("@Status") = arg3
        map("@Setup") = arg4
        map("@Id") = arg5

        Dim MyConnection As SqlConnection
        'Dim MyDataReader As SqlDataReader
        Dim MyCommand As SqlCommand


        Try
            MyConnection = GET_OPEN_CONNECTION()

            MyCommand = BUILD_COMMAND(procName, MyConnection, map)

            MyCommand.ExecuteNonQuery()

            MyConnection.Close()

        Catch ex As Exception

            'if connection is open, close it,
            'clear the 

        End Try

    End Sub

    <Runtime.CompilerServices.MethodImpl(Runtime.CompilerServices.MethodImplOptions.Synchronized)> _
    Public Shared Function FET_SIMULATIONS(ByVal arg1 As Object) As Collection
        Dim col As New Collection


        Dim procName As String = "FET_SIMULATIONS"
        Dim map As New Hashtable

        map("@ID") = arg1


        Dim MyConnection As SqlConnection
        Dim MyDataReader As SqlDataReader
        Dim MyCommand As SqlCommand


        Try
            MyConnection = GET_OPEN_CONNECTION()

            MyCommand = BUILD_COMMAND(procName, MyConnection, map)

            MyDataReader = MyCommand.ExecuteReader()

            Dim _sim As Base

            While (MyDataReader.Read())

                Dim id As String = CType(MyDataReader("S_ID"), Integer)
                Dim name As String = CType(MyDataReader("S_NAME"), String)
                Dim desc As String = CType(MyDataReader("S_DESC"), String)
                Dim setup As String = CType(MyDataReader("S_SETUP"), String)

                _sim = New Base(id, name, desc)
                'does some merging in some cases
                _sim.Setup = New SimulationSetup().XMLToSetup(setup)

                If (_sim.Setup.Update) Then
                    'in case training, injury or player was deleted, save the new stuff behind the scenes
                    UPD_SIMULATION(_sim.Name, _sim.Desc, _sim.Setup.SetupToXML(), _sim.ID)

                End If

                col.Add(_sim, id)


            End While


            MyConnection.Close()


            Return col
        Catch ex As Exception

        End Try

        Return Nothing

    End Function

    <Runtime.CompilerServices.MethodImpl(Runtime.CompilerServices.MethodImplOptions.Synchronized)> _
    Public Shared Function FET_SIMULATION(ByVal arg1 As Object) As Base

        Dim procName As String = "FET_SIMULATION"
        Dim map As New Hashtable

        map("@ID") = arg1


        Dim MyConnection As SqlConnection
        Dim MyDataReader As SqlDataReader
        Dim MyCommand As SqlCommand


        Try
            MyConnection = GET_OPEN_CONNECTION()

            MyCommand = BUILD_COMMAND(procName, MyConnection, map)

            MyDataReader = MyCommand.ExecuteReader()

            Dim _sim As Base

            While (MyDataReader.Read())

                Dim id As String = CType(MyDataReader("S_ID"), Integer)
                Dim name As String = CType(MyDataReader("S_NAME"), String)
                Dim desc As String = CType(MyDataReader("S_DESC"), String)
                Dim setup As String = CType(MyDataReader("S_SETUP"), String)

                _sim = New Base(id, name, desc)

                _sim.Setup = New SimulationSetup().XMLToSetup(setup)

            End While


            MyConnection.Close()

            Return _sim
        Catch ex As Exception

        End Try

        Return Nothing
    End Function

    <Runtime.CompilerServices.MethodImpl(Runtime.CompilerServices.MethodImplOptions.Synchronized)> _
    Public Shared Sub DELETE_SIMULATION(ByVal arg1 As Object)

        Dim procName As String = "[DELETE_SIMULATION]"
        Dim map As New Hashtable

        map("@ID") = arg1


        Dim MyConnection As SqlConnection
        'Dim MyDataReader As SqlDataReader
        Dim MyCommand As SqlCommand


        Try
            MyConnection = GET_OPEN_CONNECTION()

            MyCommand = BUILD_COMMAND(procName, MyConnection, map)

            MyCommand.ExecuteNonQuery()

            MyConnection.Close()

        Catch ex As Exception

        End Try

    End Sub

    <Runtime.CompilerServices.MethodImpl(Runtime.CompilerServices.MethodImplOptions.Synchronized)> _
   Public Shared Sub UPD_SIMULATION(ByVal arg1 As Object, ByVal arg2 As Object, ByVal arg3 As Object, ByVal arg4 As Object)

        Dim procName As String = "UPD_SIMULATION"
        Dim map As New Hashtable

        map("@Name") = arg1
        map("@Desc") = arg2
        map("@Setup") = arg3
        map("@Id") = arg4

        Dim MyConnection As SqlConnection
        'Dim MyDataReader As SqlDataReader
        Dim MyCommand As SqlCommand


        Try
            MyConnection = GET_OPEN_CONNECTION()

            MyCommand = BUILD_COMMAND(procName, MyConnection, map)

            MyCommand.ExecuteNonQuery()

            MyConnection.Close()
        Catch ex As Exception

        End Try

    End Sub


#End Region


#Region "TRAINING"

    <Runtime.CompilerServices.MethodImpl(Runtime.CompilerServices.MethodImplOptions.Synchronized)> _
    Public Shared Sub INS_NEW_DEFAULT_TRAINING(ByVal arg1 As Object, ByVal arg2 As Object, ByVal arg3 As Object, ByVal arg4 As Object)

        Dim procName As String = "INS_NEW_DEFAULT_TRAINING"
        Dim map As New Hashtable

        map("@Name") = arg1
        map("@Desc") = arg2
        map("@Setup") = arg3
        map("@Id") = arg4

        Dim MyConnection As SqlConnection
        'Dim MyDataReader As SqlDataReader
        Dim MyCommand As SqlCommand

        Try
            MyConnection = GET_OPEN_CONNECTION()

            MyCommand = BUILD_COMMAND(procName, MyConnection, map)

            MyCommand.ExecuteNonQuery()

            MyConnection.Close()
        Catch ex As Exception

        End Try


    End Sub

    <Runtime.CompilerServices.MethodImpl(Runtime.CompilerServices.MethodImplOptions.Synchronized)> _
    Public Shared Function FET_TRAININGS(ByVal arg1 As Object) As Collection
        Dim col As New Collection

        Dim procName As String = "FET_TRAININGS"
        Dim map As New Hashtable

        map("@ID") = arg1

        Dim MyConnection As SqlConnection
        Dim MyDataReader As SqlDataReader
        Dim MyCommand As SqlCommand


        Try
            MyConnection = GET_OPEN_CONNECTION()

            MyCommand = BUILD_COMMAND(procName, MyConnection, map)

            MyDataReader = MyCommand.ExecuteReader()

            Dim _training As Base
            While (MyDataReader.Read())

                Dim id As String = CType(MyDataReader("T_ID"), Integer)
                Dim name As String = CType(MyDataReader("T_NAME"), String)
                Dim desc As String = CType(MyDataReader("T_DESC"), String)
                Dim setup As String = CType(MyDataReader("T_SETUP"), String)
                _training = New Base(id, name, desc)
                _training.Setup = New TrainingSetup().XMLToSetup(setup)

                UPD_TRAINING(_training.Name, _training.Desc, _training.Setup.SetupToXML(), _training.ID)

                col.Add(_training, id)

            End While
            MyConnection.Close()

            Return col

        Catch ex As Exception

        End Try

        Return Nothing
    End Function

    <Runtime.CompilerServices.MethodImpl(Runtime.CompilerServices.MethodImplOptions.Synchronized)> _
    Public Shared Function FET_TRAINING(ByVal arg1 As Object) As Base

        Dim procName As String = "FET_TRAINING"
        Dim map As New Hashtable

        map("@ID") = arg1

        Dim MyConnection As SqlConnection
        Dim MyDataReader As SqlDataReader
        Dim MyCommand As SqlCommand

        Try
            MyConnection = GET_OPEN_CONNECTION()

            MyCommand = BUILD_COMMAND(procName, MyConnection, map)

            MyDataReader = MyCommand.ExecuteReader()

            Dim _training As Base
            While (MyDataReader.Read())

                Dim id As String = CType(MyDataReader("T_ID"), Integer)
                Dim name As String = CType(MyDataReader("T_NAME"), String)
                Dim desc As String = CType(MyDataReader("T_DESC"), String)
                Dim setup As String = CType(MyDataReader("T_SETUP"), String)
                _training = New Base(id, name, desc)
                _training.Setup = New TrainingSetup().XMLToSetup(setup)

            End While


            MyConnection.Close()

            Return _training
        Catch ex As Exception

        End Try

        Return Nothing
    End Function

    <Runtime.CompilerServices.MethodImpl(Runtime.CompilerServices.MethodImplOptions.Synchronized)> _
    Public Shared Sub DELETE_TRAINING(ByVal arg1 As Object)

        Dim procName As String = "DELETE_TRAINING"
        Dim map As New Hashtable

        map("@ID") = arg1


        Dim MyConnection As SqlConnection
        'Dim MyDataReader As SqlDataReader
        Dim MyCommand As SqlCommand


        Try
            MyConnection = GET_OPEN_CONNECTION()

            MyCommand = BUILD_COMMAND(procName, MyConnection, map)

            MyCommand.ExecuteNonQuery()

            MyConnection.Close()
        Catch ex As Exception

        End Try


    End Sub

    <Runtime.CompilerServices.MethodImpl(Runtime.CompilerServices.MethodImplOptions.Synchronized)> _
    Public Shared Sub UPD_TRAINING(ByVal arg1 As Object, ByVal arg2 As Object, ByVal arg3 As Object, ByVal arg4 As Object)

        Dim procName As String = "UPD_TRAINING"
        Dim map As New Hashtable

        map("@Name") = arg1
        map("@Desc") = arg2
        map("@Setup") = arg3
        map("@Id") = arg4

        Dim MyConnection As SqlConnection
        'Dim MyDataReader As SqlDataReader
        Dim MyCommand As SqlCommand


        Try
            MyConnection = GET_OPEN_CONNECTION()

            MyCommand = BUILD_COMMAND(procName, MyConnection, map)

            MyCommand.ExecuteNonQuery()

            MyConnection.Close()

        Catch ex As Exception

        End Try

    End Sub


#End Region


#Region "INJURY"

    <Runtime.CompilerServices.MethodImpl(Runtime.CompilerServices.MethodImplOptions.Synchronized)> _
    Public Shared Sub INS_NEW_DEFAULT_INJURY(ByVal arg1 As Object, ByVal arg2 As Object, ByVal arg3 As Object, ByVal arg4 As Object)

        Dim procName As String = "INS_NEW_DEFAULT_INJURY"
        Dim map As New Hashtable

        map("@Name") = arg1
        map("@Desc") = arg2
        map("@Setup") = arg3
        map("@Id") = arg4

        Dim MyConnection As SqlConnection
        'Dim MyDataReader As SqlDataReader
        Dim MyCommand As SqlCommand


        Try
            MyConnection = GET_OPEN_CONNECTION()

            MyCommand = BUILD_COMMAND(procName, MyConnection, map)

            MyCommand.ExecuteNonQuery()

            MyConnection.Close()
        Catch ex As Exception

        End Try


    End Sub

    <Runtime.CompilerServices.MethodImpl(Runtime.CompilerServices.MethodImplOptions.Synchronized)> _
    Public Shared Function FET_INJURIES(ByVal arg1 As Object) As Collection

        Dim col As New Collection
        Dim procName As String = "FET_INJURIES"
        Dim map As New Hashtable

        map("@ID") = arg1


        Dim MyConnection As SqlConnection
        Dim MyDataReader As SqlDataReader
        Dim MyCommand As SqlCommand

        Try
            MyConnection = GET_OPEN_CONNECTION()

            MyCommand = BUILD_COMMAND(procName, MyConnection, map)

            MyDataReader = MyCommand.ExecuteReader()

            Dim _injury As Base

            While (MyDataReader.Read())

                Dim id As String = CType(MyDataReader("I_ID"), Integer)
                Dim name As String = CType(MyDataReader("I_NAME"), String)
                Dim desc As String = CType(MyDataReader("I_DESC"), String)
                Dim setup As String = CType(MyDataReader("I_SETUP"), String)

                _injury = New Base(id, name, desc)
                _injury.Setup = New InjurySetup().XMLToSetup(setup)

                col.Add(_injury, id)
            End While


            MyConnection.Close()

            Return col

        Catch ex As Exception

        End Try
        Return Nothing

    End Function

    <Runtime.CompilerServices.MethodImpl(Runtime.CompilerServices.MethodImplOptions.Synchronized)> _
    Public Shared Function FET_INJURY(ByVal arg1 As Object) As Base
        Dim procName As String = "FET_INJURY"
        Dim map As New Hashtable

        map("@ID") = arg1


        Dim MyConnection As SqlConnection
        Dim MyDataReader As SqlDataReader
        Dim MyCommand As SqlCommand


        Try
            MyConnection = GET_OPEN_CONNECTION()

            MyCommand = BUILD_COMMAND(procName, MyConnection, map)

            MyDataReader = MyCommand.ExecuteReader()

            Dim _injury As Base

            While (MyDataReader.Read())

                Dim id As String = CType(MyDataReader("I_ID"), Integer)
                Dim name As String = CType(MyDataReader("I_NAME"), String)
                Dim desc As String = CType(MyDataReader("I_DESC"), String)
                Dim setup As String = CType(MyDataReader("I_SETUP"), String)

                _injury = New Base(id, name, desc)
                _injury.Setup = New InjurySetup().XMLToSetup(setup)

            End While


            MyConnection.Close()


            Return _injury
        Catch ex As Exception

        End Try

        Return Nothing
    End Function

    <Runtime.CompilerServices.MethodImpl(Runtime.CompilerServices.MethodImplOptions.Synchronized)> _
    Public Shared Sub DELETE_INJURY(ByVal arg1 As Object)

        Dim procName As String = "DELETE_INJURY"
        Dim map As New Hashtable

        map("@ID") = arg1


        Dim MyConnection As SqlConnection
        'Dim MyDataReader As SqlDataReader
        Dim MyCommand As SqlCommand

        Try
            MyConnection = GET_OPEN_CONNECTION()

            MyCommand = BUILD_COMMAND(procName, MyConnection, map)

            MyCommand.ExecuteNonQuery()

            MyConnection.Close()

        Catch ex As Exception

        End Try

    End Sub

    <Runtime.CompilerServices.MethodImpl(Runtime.CompilerServices.MethodImplOptions.Synchronized)> _
    Public Shared Sub UPD_INJURY(ByVal arg1 As Object, ByVal arg2 As Object, ByVal arg3 As Object, ByVal arg4 As Object)

        Dim procName As String = "UPD_INJURY"
        Dim map As New Hashtable

        map("@Name") = arg1
        map("@Desc") = arg2
        map("@Setup") = arg3
        map("@Id") = arg4

        Dim MyConnection As SqlConnection
        'Dim MyDataReader As SqlDataReader
        Dim MyCommand As SqlCommand


        Try
            MyConnection = GET_OPEN_CONNECTION()

            MyCommand = BUILD_COMMAND(procName, MyConnection, map)

            MyCommand.ExecuteNonQuery()

            MyConnection.Close()
        Catch ex As Exception

        End Try


    End Sub
#End Region


#Region "PLAYER"

    <Runtime.CompilerServices.MethodImpl(Runtime.CompilerServices.MethodImplOptions.Synchronized)> _
    Public Shared Sub INS_NEW_DEFAULT_PLAYER(ByVal arg1 As Object, ByVal arg2 As Object, ByVal arg3 As Object, ByVal arg4 As Object)

        Dim procName As String = "INS_NEW_DEFAULT_PLAYER"
        Dim map As New Hashtable

        map("@Name") = arg1
        map("@Desc") = arg2
        map("@Setup") = arg3
        map("@Id") = arg4

        Dim MyConnection As SqlConnection
        'Dim MyDataReader As SqlDataReader
        Dim MyCommand As SqlCommand


        Try
            MyConnection = GET_OPEN_CONNECTION()

            MyCommand = BUILD_COMMAND(procName, MyConnection, map)

            MyCommand.ExecuteNonQuery()

            MyConnection.Close()
        Catch ex As Exception

        End Try


    End Sub

    <Runtime.CompilerServices.MethodImpl(Runtime.CompilerServices.MethodImplOptions.Synchronized)> _
    Public Shared Function FET_PLAYERS(ByVal arg1 As Object) As Collection
        Dim col As New Collection


        Dim procName As String = "FET_PLAYERS"
        Dim map As New Hashtable

        map("@ID") = arg1


        Dim MyConnection As SqlConnection
        Dim MyDataReader As SqlDataReader
        Dim MyCommand As SqlCommand


        Try
            MyConnection = GET_OPEN_CONNECTION()

            MyCommand = BUILD_COMMAND(procName, MyConnection, map)

            MyDataReader = MyCommand.ExecuteReader()

            Dim _player As Base

            While (MyDataReader.Read())

                Dim id As String = CType(MyDataReader("P_ID"), Integer)
                Dim name As String = CType(MyDataReader("P_NAME"), String)
                Dim desc As String = CType(MyDataReader("P_DESC"), String)
                Dim setup As String = CType(MyDataReader("P_SETUP"), String)

                _player = New Base(id, name, desc)
                'does some merging in some cases
                _player.Setup = New PlayerSetup().XMLToSetup(setup)

                If (_player.Setup.Update) Then
                    'in case an injury was deleted, save the new stuff behind the scenes
                    UPD_PLAYER(_player.Name, _player.Desc, _player.Setup.SetupToXML(), _player.ID)

                End If

                col.Add(_player, id)

            End While


            MyConnection.Close()


            Return col
        Catch ex As Exception

        End Try

        Return Nothing
    End Function

    <Runtime.CompilerServices.MethodImpl(Runtime.CompilerServices.MethodImplOptions.Synchronized)> _
    Public Shared Function FET_ALL_PLAYERS() As Collection
        Dim col As New Collection


        Dim procName As String = "FET_ALL_PLAYERS"
        Dim map As New Hashtable


        Dim MyConnection As SqlConnection
        Dim MyDataReader As SqlDataReader
        Dim MyCommand As SqlCommand


        Try
            MyConnection = GET_OPEN_CONNECTION()

            MyCommand = BUILD_COMMAND(procName, MyConnection, map)

            MyDataReader = MyCommand.ExecuteReader()

            Dim _player As Base

            While (MyDataReader.Read())

                Dim id As String = CType(MyDataReader("P_ID"), Integer)
                Dim name As String = CType(MyDataReader("P_NAME"), String)
                Dim desc As String = CType(MyDataReader("P_DESC"), String)
                Dim setup As String = CType(MyDataReader("P_SETUP"), String)

                _player = New Base(id, name, desc)
                'does some merging in some cases
                _player.Setup = New PlayerSetup().XMLToSetup(setup)

                If (_player.Setup.Update) Then
                    'in case an injury was deleted, save the new stuff behind the scenes
                    UPD_PLAYER(_player.Name, _player.Desc, _player.Setup.SetupToXML(), _player.ID)

                End If

                col.Add(_player, id)

            End While


            MyConnection.Close()


            Return col
        Catch ex As Exception

        End Try

        Return Nothing
    End Function

    <Runtime.CompilerServices.MethodImpl(Runtime.CompilerServices.MethodImplOptions.Synchronized)> _
    Public Shared Function FET_PLAYER(ByVal arg1 As Object) As Base

        Dim procName As String = "FET_PLAYER"
        Dim map As New Hashtable

        map("@ID") = arg1


        Dim MyConnection As SqlConnection
        Dim MyDataReader As SqlDataReader
        Dim MyCommand As SqlCommand


        Try
            MyConnection = GET_OPEN_CONNECTION()

            MyCommand = BUILD_COMMAND(procName, MyConnection, map)

            MyDataReader = MyCommand.ExecuteReader()

            Dim _player As Base

            While (MyDataReader.Read())

                Dim id As String = CType(MyDataReader("P_ID"), Integer)
                Dim name As String = CType(MyDataReader("P_NAME"), String)
                Dim desc As String = CType(MyDataReader("P_DESC"), String)
                Dim setup As String = CType(MyDataReader("P_SETUP"), String)

                _player = New Base(id, name, desc)
                _player.Setup = New PlayerSetup().XMLToSetup(setup)

            End While


            MyConnection.Close()

            Return _player
        Catch ex As Exception

        End Try

        Return Nothing

    End Function

    <Runtime.CompilerServices.MethodImpl(Runtime.CompilerServices.MethodImplOptions.Synchronized)> _
    Public Shared Sub DELETE_PLAYER(ByVal arg1 As Object)

        Dim procName As String = "DELETE_PLAYER"
        Dim map As New Hashtable

        map("@ID") = arg1

        Dim MyConnection As SqlConnection

        Dim MyCommand As SqlCommand

        Try
            MyConnection = GET_OPEN_CONNECTION()

            MyCommand = BUILD_COMMAND(procName, MyConnection, map)

            MyCommand.ExecuteNonQuery()
            MyConnection.Close()
        Catch ex As Exception

        End Try


    End Sub

    Public Shared Sub UPD_PLAYER(ByVal arg1 As Object, ByVal arg2 As Object, ByVal arg3 As Object, ByVal arg4 As Object)

        Dim procName As String = "UPD_PLAYER"
        Dim map As New Hashtable

        map("@Name") = arg1
        map("@Desc") = arg2
        map("@Setup") = arg3
        map("@Id") = arg4

        Dim MyConnection As SqlConnection
        'Dim MyDataReader As SqlDataReader
        Dim MyCommand As SqlCommand

        Try
            MyConnection = GET_OPEN_CONNECTION()

            MyCommand = BUILD_COMMAND(procName, MyConnection, map)

            MyCommand.ExecuteNonQuery()
           
            MyConnection.Close()
        Catch ex As Exception

        End Try
        

    End Sub
#End Region


#Region "REPORT"

    <Runtime.CompilerServices.MethodImpl(Runtime.CompilerServices.MethodImplOptions.Synchronized)> _
    Public Shared Function INS_NEW_REPORT_ENTRY(ByVal arg1 As Object) As Integer

        Dim procName As String = "INS_NEW_REPORT_ENTRY"
        Dim map As New Hashtable

        map("@sId") = arg1



        Dim MyConnection As SqlConnection
        Dim MyDataReader As SqlDataReader
        Dim MyCommand As SqlCommand

        Try
            MyConnection = GET_OPEN_CONNECTION()

            MyCommand = BUILD_COMMAND(procName, MyConnection, map)

            MyDataReader = MyCommand.ExecuteReader()
            Dim id As Integer
            While (MyDataReader.Read())

                id = CType(MyDataReader("SCOPE_IDENTITY"), Integer)

            End While


            MyConnection.Close()
            Return id
        Catch ex As Exception

        End Try

        Return -999
    End Function

    <Runtime.CompilerServices.MethodImpl(Runtime.CompilerServices.MethodImplOptions.Synchronized)> _
    Public Shared Sub UPD_REPORT_ENTRY_FAILED(ByVal arg1 As Object)

        Dim procName As String = "INS_NEW_REPORT_ENTRY"
        Dim map As New Hashtable

        map("@rId") = arg1


        Dim MyConnection As SqlConnection
        'Dim MyDataReader As SqlDataReader
        Dim MyCommand As SqlCommand

        Try
            MyConnection = GET_OPEN_CONNECTION()

            MyCommand = BUILD_COMMAND(procName, MyConnection, map)

            MyCommand.ExecuteNonQuery()

            MyConnection.Close()
        Catch ex As Exception

        End Try


    End Sub

    <Runtime.CompilerServices.MethodImpl(Runtime.CompilerServices.MethodImplOptions.Synchronized)> _
    Public Shared Function UPD_REPORT_ENTRY_SUCCESS(ByVal arg1 As Object, ByVal arg2 As Object) As Integer

        Dim procName As String = "UPD_REPORT_ENTRY_SUCCESS"
        Dim map As New Hashtable

        map("@rId") = arg1
        map("@sData") = arg2


        Dim MyConnection As SqlConnection

        Dim MyCommand As SqlCommand

        Try
            MyConnection = GET_OPEN_CONNECTION()

            MyCommand = BUILD_COMMAND(procName, MyConnection, map)

            MyCommand.ExecuteNonQuery()

            MyConnection.Close()
            'Return id
        Catch ex As Exception

        End Try

    End Function

    <Runtime.CompilerServices.MethodImpl(Runtime.CompilerServices.MethodImplOptions.Synchronized)> _
    Public Shared Function FET_REPORTS(ByVal arg1 As Object) As Collection
        Dim col As New Collection

        Dim procName As String = "FET_REPORTS"
        Dim map As New Hashtable

        map("@ID") = arg1

        Dim MyConnection As SqlConnection
        Dim MyDataReader As SqlDataReader
        Dim MyCommand As SqlCommand

        Try
            MyConnection = GET_OPEN_CONNECTION()

            MyCommand = BUILD_COMMAND(procName, MyConnection, map)

            MyDataReader = MyCommand.ExecuteReader()
            Dim rpt As Reporting

            While (MyDataReader.Read())
                rpt = New Reporting
                rpt.ReportID = CType(MyDataReader("R_ID"), Integer)
                rpt.SimulationID = CType(MyDataReader("S_ID"), Integer)
                rpt.SimulationName = CType(MyDataReader("S_NAME"), String)

                If Not MyDataReader("R_DATA") Is DBNull.Value Then
                    rpt.ReportData = CType(MyDataReader("R_DATA"), Byte())
                End If

                If Not MyDataReader("R_TIMESTAMP") Is DBNull.Value Then
                    rpt.TimeStamp = CType(MyDataReader("R_TIMESTAMP"), DateTime)
                End If



                rpt.ReportStatus = CType(MyDataReader("R_STATUS"), String)
                col.Add(rpt, rpt.ReportID)
            End While


            MyConnection.Close()

            Return col
        Catch ex As Exception

        End Try
        Return Nothing
    End Function

    <Runtime.CompilerServices.MethodImpl(Runtime.CompilerServices.MethodImplOptions.Synchronized)> _
    Public Shared Function FET_REPORT(ByVal arg1 As Object) As Reporting
        Dim col As New Collection

        Dim procName As String = "FET_REPORT"
        Dim map As New Hashtable

        map("@ID") = arg1

        Dim MyConnection As SqlConnection
        Dim MyDataReader As SqlDataReader
        Dim MyCommand As SqlCommand

        Try
            MyConnection = GET_OPEN_CONNECTION()

            MyCommand = BUILD_COMMAND(procName, MyConnection, map)

            MyDataReader = MyCommand.ExecuteReader()
            Dim rpt As Reporting

            While (MyDataReader.Read())
                rpt = New Reporting
                rpt.ReportID = CType(MyDataReader("R_ID"), Integer)
                rpt.SimulationID = CType(MyDataReader("S_ID"), Integer)

                If Not MyDataReader("R_DATA") Is DBNull.Value Then
                    rpt.ReportData = CType(MyDataReader("R_DATA"), Byte())
                End If

                If Not MyDataReader("R_TIMESTAMP") Is DBNull.Value Then
                    rpt.TimeStamp = CType(MyDataReader("R_TIMESTAMP"), DateTime)
                End If



                rpt.ReportStatus = CType(MyDataReader("R_STATUS"), String)
                col.Add(rpt, rpt.ReportID)
            End While


            MyConnection.Close()

            Return col.Item(1)
        Catch ex As Exception

        End Try
        Return Nothing
    End Function

   
#End Region


#Region "USER"

    <Runtime.CompilerServices.MethodImpl(Runtime.CompilerServices.MethodImplOptions.Synchronized)> _
    Public Shared Function FET_USER(ByVal arg1 As Object, ByVal arg2 As Object) As User

        Dim procName As String = "FET_USER"
        Dim map As New Hashtable

        map("@U_NAME") = arg1
        map("@U_PASSWORD") = arg2


        Dim MyConnection As SqlConnection
        Dim MyDataReader As SqlDataReader
        Dim MyCommand As SqlCommand

        Try
            MyConnection = GET_OPEN_CONNECTION()

            MyCommand = BUILD_COMMAND(procName, MyConnection, map)

            MyDataReader = MyCommand.ExecuteReader()

            Dim _user As User

            While (MyDataReader.Read())

                Dim id As String = CType(MyDataReader("U_ID"), Integer)
                Dim name As String = CType(MyDataReader("U_NAME"), String)
                Dim pass As String = CType(MyDataReader("U_PASSWORD"), String)
                Dim email As String = CType(MyDataReader("U_EMAIL"), String)
                Dim setup As String = CType(MyDataReader("U_SETUP"), String)

                _user = New User(id, name, pass, email)

            End While

            MyConnection.Close()

            Return _user
        Catch ex As Exception

        End Try

        Return Nothing

    End Function

    <Runtime.CompilerServices.MethodImpl(Runtime.CompilerServices.MethodImplOptions.Synchronized)> _
    Public Shared Function INS_NEW_USER(ByVal arg1 As Object, ByVal arg2 As Object, ByVal arg3 As Object, ByVal arg4 As Object) As Boolean

        Dim procName As String = "INS_NEW_USER"
        Dim map As New Hashtable

        map("@U_NAME") = arg1
        map("@U_PASSWORD") = arg2
        map("@U_EMAIL") = arg3
        map("@U_SETUP") = arg4

        Dim MyConnection As SqlConnection
        Dim MyDataReader As SqlDataReader
        Dim MyCommand As SqlCommand

        Try
            MyConnection = GET_OPEN_CONNECTION()

            MyCommand = BUILD_COMMAND(procName, MyConnection, map)

            MyDataReader = MyCommand.ExecuteReader()
            Dim val As Integer = -1
            Dim max As Integer = 0
            While (MyDataReader.Read())

                val = CType(MyDataReader("SCOPE_IDENTITY"), Integer)
                max += 1
            End While
            MyConnection.Close()

            Return True

        Catch ex As Exception

        End Try
        Return False
    End Function

    <Runtime.CompilerServices.MethodImpl(Runtime.CompilerServices.MethodImplOptions.Synchronized)> _
    Public Shared Function FET_USER_BY_ID(ByVal arg1 As Object) As User

        Dim procName As String = "FET_USER_BY_ID"
        Dim map As New Hashtable

        map("@U_ID") = arg1


        Dim MyConnection As SqlConnection
        Dim MyDataReader As SqlDataReader
        Dim MyCommand As SqlCommand

        Try
            MyConnection = GET_OPEN_CONNECTION()
            MyCommand = BUILD_COMMAND(procName, MyConnection, map)

            'MyCommand.ExecuteNonQuery()
            MyDataReader = MyCommand.ExecuteReader()
            Dim _user As User
            While (MyDataReader.Read())

                Dim id As String = CType(MyDataReader("U_ID"), Integer)
                Dim name As String = CType(MyDataReader("U_NAME"), String)
                Dim pass As String = CType(MyDataReader("U_PASSWORD"), String)
                Dim email As String = CType(MyDataReader("U_EMAIL"), String)
                Dim setup As String = CType(MyDataReader("U_SETUP"), String)

                _user = New User(id, name, pass, email)
            End While

            'if max = 2, then we just inserted a user name that exists

            MyConnection.Close()

            Return _user

        Catch ex As Exception

        End Try
        Return Nothing

    End Function

#End Region



End Class
